<template>
  <div>
    <basic-container>数据 / 产品 / 每日唤醒用户数据</basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
      >
        <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click.stop="openDialog(1)"
            >添加</el-button
          >
        </template>

        <!-- 自定义操作栏 -->
        <template slot-scope="scope" slot="menu">
          <el-button
            icon="el-icon-edit"
            type="text"
            size="small"
            @click.stop="openDialog(2, scope.row)"
            >编辑</el-button
          >
          <el-button
            icon="el-icon-danger"
            type="text"
            size="small"
            @click.stop="rowDel(scope.row)"
            >删除</el-button
          >
        </template>
      </avue-crud>
    </basic-container>
    <!-- 添加弹出框start -->

    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
      :modal="false"
    >
      <el-form ref="form" :model="from" label-width="100px">
        <el-form-item label="用户手机号">
          <el-input type="textarea" v-model="from.rouse_phone"></el-input>
        </el-form-item>

        <el-form-item label="选择日期">
          <el-date-picker
            v-model="from.date"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { ddUrl } from "@/config/env";
import { handleExcelCat } from "@/api/exportCommon";
import { appStatisticsEverydayRouseUser as baseUrl } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      title: "添加",
      dialogVisible: false,
      from: {
        rouse_phone: "",
        date: [],
      },
      //   快捷选择时间
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },

      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {},
      option: {
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: true,
        selection: true,
border: true,
        index: true,
        indexLabel: "序号",
        column: [
          {
            label: "开始日期",
            prop: "start_date",
            type: "date",
          },
          {
            label: "结束日期",
            type: "date",
            prop: "end_date",
          },
          {
            label: "唤醒用户数",
            prop: "rouse_num",
            type: "input",
          },
          {
            label: "统计状态",
            prop: "stats_type",
            type: "select",
            dicData: [
              {
                label: "未开始",
                value: 0,
              },
              {
                label: "进行中",
                value: 1,
              },
              {
                label: "已结束",
                value: 2,
              },
            ],
          },
          {
            label: "选择日期",
            type: "daterange",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            hide: true,
            search: true,
          },
        ],
      },
    };
  },
  methods: {
    openDialog(type, edit) {
      if (type == 1) {
        this.title = "添加";
        this.from = {
          rouse_phone: "",
          date: [],
        };
      } else {
        this.title = "编辑";
        console.log(type);
        this.from = {
          rouse_phone: edit.rouse_phone.join("\n"),
          date: [edit.start_date, edit.end_date],
          id: edit.id,
        };
        //查询信息
      }
      this.dialogVisible = true;
    },
    async handleAdd() {
      if (!this.from.date||this.from.date.length < 2) {
        this.$message.error("请选择一个日期区间");
        return;
      }
      if (/^\s*$/g.test(this.from.rouse_phone)) {
        this.$message.error("请填写手机号");
        return;
      }

      let request = {
        start_date: this.from.date[0],
        end_date: this.from.date[1],
        rouse_phone: this.from.rouse_phone.split(/\n/),
      };
      let data = await common(`${baseUrl.save}`, request, undefined, "POST");
      if (data.code == 1) {
        this.dialogVisible = false;
        this.getList();
      }
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseUrl.pageAndParam,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    } /**刷新 */,
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      if (params.daterange) {
        params.start_date = `>${params.daterange[0]}`;
        params.end_date = `<${params.daterange[1]}`;
      }
      delete params.daterange
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
    /**删除 */
      rowDel (row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            `${baseUrl.delete}`,
            { id: row.id },
            null,
            "GET"
          );
          if (response == null) {
            return ;
          }
        })
        .then(() => {
          
          this.getList();
        });
    },
    /**修改 */
    async rowUpdate () {
      console.log(this.from)
      if(!this.from.date||this.from.date.length<2){
          this.$message.error('请选择一个日期区间')
          return
      }
      if(!this.from.rouse_phone||/^\s*$/g.test(this.from.rouse_phone)){
          this.$message.error('请填写手机号')
          return
      }

      let request = {
          start_date :  this.from.date[0],
          end_date : this.from.date[1],
          rouse_phone : this.from.rouse_phone.split(/\n/),
          id : this.from.id
      }
  
      let data = await common(`${baseUrl.update}`, request, null, "POST");
      if (data == null) {
        this.dialogVisible = false     
        return 
      }
      this.dialogVisible = false      
      this.getList();
    }
  },
};
</script>